/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.activemq;

import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

import static yhao.infra.feature.activemq.ConfigFactory.*;

/**
 * activemq配置类
 *
 * @author yoara
 */
public class ActiveMqConfig {
    @Autowired
    private Environment environment;

    @Bean("amqConnectionFactory")
    public ActiveMQConnectionFactory amqConnectionFactory(){
        return makeActiveMQConnectionFactory(environment,BASIC_ACTIVEMQ_URL);
    }

    @Bean("connectionFactory")
    public CachingConnectionFactory connectionFactory(ActiveMQConnectionFactory amqConnectionFactory){
        return makeCachingConnectionFactory(environment,amqConnectionFactory,BASIC_ACTIVEMQ_SESSION_SIZE);
    }

    @Bean("jmsTemplate")
    public JmsTemplate jmsTemplate(CachingConnectionFactory connectionFactory){
        return makeJmsTemplate(connectionFactory);
    }

    /************ 代码参考实现 ************/
    //@Bean("amqConnectionFactoryDurableTopic")
    //public ActiveMQConnectionFactory amqConnectionFactoryDurableTopic(){
    //    ActiveMQConnectionFactory amqConnectionFactory = new ActiveMQConnectionFactory();
    //    amqConnectionFactory.setBrokerURL(environment.getProperty("basic.activemq.url"));
    //    amqConnectionFactory.setConnectResponseTimeout(5000);
    //    amqConnectionFactory.setSendTimeout(10000);
    //    amqConnectionFactory.setTrustAllPackages(true);
    //    return amqConnectionFactory;
    //}
    //
    //@Bean("connectionFactoryDurableTopic")
    //public CachingConnectionFactory connectionFactoryDurableTopic(
    //        ActiveMQConnectionFactory amqConnectionFactoryDurableTopic){
    //    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(amqConnectionFactoryDurableTopic);
    //    connectionFactory.setSessionCacheSize(
    //            environment.getProperty("basic.activemq.durableTopicSessionSize",Integer.class));
    //    connectionFactory.setClientId("testId");
    //    return connectionFactory;
    //}
}
